package cn.cs.service.impl;

import cn.cs.client.ExpensesExplainClient;
import cn.cs.client.RestFaultTypeClient;
import cn.cs.common.Constants;
import cn.cs.common.EmptyUtils;
import cn.cs.common.Page;
import cn.cs.pojo.ExpensesExplain;
import cn.cs.pojo.FaultType;
import cn.cs.pojo.ServiceType;
import cn.cs.service.ExpensesExplainService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ExpensesExplainServiceImpl implements ExpensesExplainService {

    @Resource
    private ExpensesExplainClient expensesExplainClient;

    @Resource
    private RestFaultTypeClient faultTypeClient;


    public ExpensesExplain getExpensesExplainById(String id) throws Exception {
        return expensesExplainClient.getExpensesExplainById(id);
    }

    public List<ExpensesExplain> getExpensesExplainListByMap(Map<String, Object> param) throws Exception {
        return expensesExplainClient.getExpensesExplainListByMap(param);
    }

    public Integer getExpensesExplainCountByMap(Map<String, Object> param) throws Exception {
        return expensesExplainClient.getExpensesExplainCountByMap(param);
    }

    public Integer addExpensesExplain(ExpensesExplain expensesExplain) throws Exception {
        expensesExplain.setCreateDate(new Date());
        return expensesExplainClient.addExpensesExplain(expensesExplain);
    }

    public Integer modifyExpensesExplain(ExpensesExplain expensesExplain) throws Exception {
        expensesExplain.setModifyDate(new Date());
        return expensesExplainClient.modifyExpensesExplain(expensesExplain);
    }

    public Integer deleteExpensesExplainById(String id) throws Exception {
        return expensesExplainClient.deleteExpensesExplainById(id);
    }

    public Page<ExpensesExplain> queryExpensesExplainPageByMap(Map<String, Object> param, Integer pageNo, Integer pageSize) throws Exception {
        Integer total = expensesExplainClient.getExpensesExplainCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<ExpensesExplain> expensesExplainList = expensesExplainClient.getExpensesExplainListByMap(param);
        for(ExpensesExplain a:expensesExplainList){
            String faultTypeId = a.getFaultTypeId();
            String serviceTypeId = a.getServiceTypeId();
            FaultType faultType = faultTypeClient.getFaultTypeByOnlyId(faultTypeId);
            if(EmptyUtils.isNotEmpty(faultType)){
                a.setFault_name(faultType.getFaultName());
            }
            ServiceType serviceType = faultTypeClient.getServiceTypeById(serviceTypeId);
            if(EmptyUtils.isNotEmpty(serviceType)){
                a.setService_name(serviceType.getServiceName());
            }
        }
        page.setRows(expensesExplainList);
        return page;
    }

    public Map<String, Object> getAreaById(String id) throws Exception {
        return expensesExplainClient.getAreaById(id);
    }


    public Map<String, Object> getFaultTypeById(String id) throws Exception {
        return expensesExplainClient.getFaultTypeById(id);
    }



}
